package hu.myai.triggers;

import hu.myai.api.tick.AbstractTickProvider;
import hu.myai.api.triggers.AbstractTrigger;
import hu.myai.indicators.pivot.LastHighIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TriggerVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This trigger calculates if the values of two lows after each other are
 * increasing, meaning a increasing trend.
 *
 * @author bozbalint
 *
 * */

public class HeadAndShouldersTrigger extends AbstractTrigger {

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * it contains the last arrived indicator
     * */
    private IndicatorVO secondIndicator;

    /**
     * it contains the first recorded indicator
     * */
    private IndicatorVO firstIndicator;

    private String lastHighID;
    public void setLastHighIndicatorParameter(double parameter) {
        // Trigger is listen on last high indicator
        lastHighID = register(new LastHighIndicator(parameter));
    }


    public HeadAndShouldersTrigger(String name, AbstractTickProvider tick) {
        super(name, tick);
    }

    @Override
    protected TriggerVO triggerCalculation() {
        IndicatorVO thirdIndicator = indicatorMap.get(lastHighID);

        // no value or the value is same as before, no aim to do check
        if (thirdIndicator == null || !thirdIndicator.isChanged())
            return null;

        if (firstIndicator == null) {
            firstIndicator = thirdIndicator;
            secondIndicator = thirdIndicator;
            return null;
        }

        TriggerVO triggerVO = null;
        if (secondIndicator.getValue() > firstIndicator.getValue()
                && secondIndicator.getValue() > thirdIndicator.getValue()) {

            triggerVO = new TriggerVO();
            triggerVO.setTradeType(-1);
            triggerVO.setRisk(5);

            log.debug("TriggerVO: " + triggerVO.toString());
        }

        firstIndicator = secondIndicator;
        secondIndicator = thirdIndicator;

        return triggerVO;
    }

}
